{extend name='common/common' /}

{block name="style"}
<style type="text/css">
    .container-fluid .btn.btn-sm{
        float: right;
        margin-left: 10px;
    }
    .container-fluid{
        padding-right: 0px;
    }
</style>
{/block}

{block name="content"}
<div class="wrapper wrapper-content animated fadeInRight">
    <!-- Panel Other -->
    <div class="ibox float-e-margins">
        <div class="ibox-title">
            <h5>公共客户列表</h5>
        </div>
        <div class="ibox-content">
			<!-- 批量分配客源 -->
            <div class="container-fluid form-inline pull-right" style="margin-top: 10px">
                <button id="distribution" class="btn btn-danger btn-sm getIDs" data-param="">批量分配</button>
            </div>
			<!-- 导入资料库客源 -->
			<div class="container-fluid form-inline pull-right" style="margin-top: 10px">
				<button data-action="{:url('Member/resMemExcel')}" data-multiple="false" data-ext="xlsx,xlsm,xltx,xltm,xlsb,xlam" data-upload="excel/<?=session('id')?>" class="btn btn-primary btn-sm">导入excel表</button>

            </div>
			<!--   添加资料库客源   -->
			<div class="container-fluid form-inline pull-right" style="margin-top: 10px">
				<a href="{:url('Member/addResMember')}">
					<button type="button" class="btn btn-primary btn-sm">
						<i class="fa fa-search"></i> 添加客户
					</button>
				</a>
            </div>
            <form id='commentForm' role="form" method="post" class="form-inline pull-left">
                <div class="content clearfix m-b">
                    <div class="form-group">
                        <label>姓名：</label>
                        <input type="text" class="form-control" id="name" name="name">
                    </div>
                    <div class="form-group">
                        <label>联系方式：</label>
                        <input type="text" class="form-control" id="phone" name="phone">
                    </div>
                    <div class="form-group">
                        <button class="btn btn-primary" type="button" style="margin-top:5px" id="search"><strong>搜 索</strong>
                        </button>
                    </div>
                </div>
            </form>
            <!--搜索框结束-->
            <div class="example-wrap">
                <div class="example">
                    <table id="cusTable">
                        <thead>
                        <th>checkbox</th>
                        <th class="text-center" data-field="id">会员ID</th>
                        <th class="text-center" data-field="name">姓名</th>
                        <th class="text-center" data-field="phone">联系电话</th>
                        <th class="text-center" data-field="adress">地址</th>
                        <th class="text-center" data-field="qq">QQ</th>
                        <th class="text-center" data-field="weixin">微信</th>
                        <th class="text-center" data-field="status">状态</th>
                        <th class="text-center" data-field="remarks">备注</th>
                        <?php if(authCheck('attachment/delattach')): ?>
                        <th class="text-center" data-field="operate">操作</th>
                        <?php endif;?>
                        </thead>
                    </table>
                </div>
            </div>
            <!-- End Example Pagination -->
        </div>
    </div>
</div>
<!-- End Panel Other -->
<!-- 获取部门结构 -->
<div class="laycontent" style="display: none;width: 300px;padding: 30px 15px;">
    <ul id="tree"></ul>
</div>
<!-- 部门下的所有职位 -->
<div class="departroles" style="display: none;width: 300px;padding: 30px 15px;">
    <div class="form-group">
        <label>请选择职位：</label>
        <select class="form-control"  name="roles" id="roles">
            <option value="0">请选择</option>
        </select>
    </div>
</div>
<!-- 获取某职位下的所有职员 -->
<div class="roleuser" style="display: none;width: 300px;padding: 30px 15px;">
    <div class="form-group">
        <label>请选择职员：</label>
        <select class="form-control"  name="user" id="user">
            <option value="0">请选择</option>
        </select>
    </div>
</div>
{/block}


{block name="script"}
<script type="text/javascript">
    function initTable() {
        //先销毁表格
        $('#cusTable').bootstrapTable('destroy');
        //初始化表格,动态从服务器加载数据
        $("#cusTable").bootstrapTable({
            method: "get",  //使用get请求到服务器获取数据
            url: "{:url('member/wholeList')}", //获取数据的地址
            striped: true,  //表格显示条纹
            pagination: true, //启动分页
            pageSize: 10,  //每页显示的记录数
            pageNumber:1, //当前第几页
            pageList: [5, 10, 15, 20, 25],  //记录数可选列表
            sidePagination: "server", //表示服务端请求
            paginationFirstText: "首页",
            paginationPreText: "上一页",
            paginationNextText: "下一页",
            paginationLastText: "尾页",
            queryParamsType : "undefined",
            columns : [{
                checkbox: true,
                formatter:function(value,row,index){
                    if (row.state == true){
                        return {
                            checked : value,//设置选中
                        };
                    }
                    return value;
                },
            }],
            queryParams: function queryParams(params) {   //设置查询参数
                var param = {
                    pageNumber: params.pageNumber,
                    pageSize: params.pageSize,
                    name:$('#name').val(),
                    phone:$('#phone').val(),
                };
                return param;
            },
            onLoadSuccess: function(res){ //加载成功时执行
                if(111 == res.code){
                    window.location.reload();
                }
                layer.msg("加载成功", {time : 1000});
            },
            onLoadError: function(){  //加载失败时执行
                layer.msg("加载数据失败");
            }
        });
    }

    $(document).ready(function () {
        //调用函数，初始化表格
        initTable();
        //当点击查询按钮的时候执行
        $("#search").bind("click", initTable);

        $('.getIDs').on('click',function(){
            var checkedOn= $('#cusTable').bootstrapTable('getSelections');
            var IDs = '';
            for(var i=0;i<checkedOn.length;i++){
                if(i == 0){
                    IDs = checkedOn[0].id;
                }else{
                    IDs += ',' + checkedOn[i].id;
                }
            }
            $('.getIDs').attr('data-param',IDs);
        })
    });
</script>

<script>
    function distribution(id) {
        $('.getIDs').attr('data-param',id);
        selectSchoolBox = layer.open({
            type: 1,
            title: '请选择部门',
            anim: 2,
            // shade:false,
            skin: 'layui-layer-molv', //加上边框
            area: ['300px', '350px'], //宽高
            content: $(".laycontent"),
        });
    }
    $(function(){
        getTree();
        layui.use(['form'], function(){
            var form = layui.form;
        });
        $('#distribution').on('click',function(){
            var param = JSON.stringify(this.getAttribute("data-param").split(","));
            if(param.length <= 4){
                layer.msg('请选择数据');
                return false;
             }
            selectSchoolBox = layer.open({
                type: 1,
                title: '请选择部门',
                anim: 2,
                // shade:false,
                skin: 'layui-layer-molv', //加上边框
                area: ['300px', '350px'], //宽高
                content: $(".laycontent"),
            });
        });
    });
    function getTree(){
        layui.use(['tree', 'layer'], function(){
            var layer = layui.layer;
            $.getJSON("{:url('Department/index')}", function(res){
                if(111 == res.code){
                    window.location.reload();
                }
                layui.tree({
                    elem: '#tree'
                    ,nodes: res.data
                    ,click: function(node){
                        var depart_id = node.id;
                        if(depart_id == '' || depart_id == null){
                            layer.msg('请选择部门',{time : 1000});
                            return false;
                        }
                        layer.closeAll();
                        //获取职位
                        $.getJSON("{:url('Department/getDepartRoles')}",{depart_id:depart_id},function(res1){
                            var app = '<option value="">请选择</option>';
                            for (var i in res1.data){
                                app += '<option value="'+res1.data[i]["id"]+'">'+res1.data[i]["role_name"]+'</option>';
                            }
                            $('#roles').html(app);
                            layer.open({
                                type:1,
                                title:"请选择职位",
                                content:$('.departroles'),
                                btn:['确认','取消'],
                                yes:function(){
                                    var role_id = $('#roles').val();
                                    if(role_id == '' || role_id == null){
                                        layer.msg('请选择职位',{time : 1000});
                                        return false;
                                    }
                                    $.getJSON("{:url('Department/getRoleUser')}",{role_id:role_id},function(res2){
                                        layer.closeAll();
                                        var app1 = '<option value="">请选择</option>';
                                        for (var i in res2.data){
                                            app1 += '<option value="'+res2.data[i]["id"]+'">'+res2.data[i]["real_name"]+'</option>';
                                        }
                                        $('#user').html(app1);
                                         layer.open({
                                             type: 1,
                                             title: '请选择职员',
                                             anim: 2,
                                             content: $(".roleuser"), btn:['确认','取消'],
                                             yes:function(){
                                                 var user_id = $('#user').val();
                                                 var param = JSON.stringify($('.getIDs').attr("data-param").split(","));
                                                 if(user_id == '' || user_id == null){
                                                     layer.msg('请选择职员',{time : 1000});
                                                     return false;
                                                 }
                                                 $.post("{:url('Member/insertMember')}",{uid:user_id,param:param},function(data){
                                                     layer.closeAll();
                                                     if(data.code == 1){
                                                         layer.msg(data.msg,{time : 1000},function(){
                                                             layer.closeAll();
                                                             initTable();
                                                         });
                                                     }else{
                                                         layer.msg(data.msg,{time : 1000},function(){
                                                             layer.closeAll();
                                                             return false;
                                                         });
                                                     }
                                                 });
                                             },
                                             btn2:function(){
                                                 layer.closeAll();
                                             }
                                        });
                                    });
                                },
                                btn2:function(){
                                    layer.closeAll();
                                }
                            });
                        });
                    }
                })
            });
        });
    }
</script>
{/block}
